;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?cityId=") != -1) {
			var reqPath = basePath+'cityManager/queryCityById.action'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var city = res.data;
						$("#uu_id").val(city.uuid);
						$("#city_name").val(city.cityName);
						$("#nick_name").val(city.nickName);
						$("#city_introduce").val(city.cityIntroduce);
						$("#city_disclaimer").val(city.disclaimer);
						$("#city_code").val(city.cityCode);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else
		{
			//初始化列表
			inicityData(0,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/technology/city/addcity.jsp';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			inicityData(0,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveCity();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateCity();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function editcity(cityId)
{
	window.location.href = jumpPath + 'view/technology/city/editcity.jsp?cityId='+cityId;
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var requsetPath = basePath + "cityManager/deleteCity.action?cityId="+id;
		send(requsetPath, "POST", "application/json", "json", null,
				function(resultData)
				{
					if (resultData.result == 'success')
					{
						// 跳转到列表
						window.location = jumpPath
								+ 'view/technology/city/citylist.jsp';
					} else
					{
						alert(resultData.errorMsg);
					}
				});
	}
}

function closeCity(id)
{
	var requsetPath = basePath + "cityManager/updateCity.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "Y"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/city/citylist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function openCity(id)
{
	var requsetPath = basePath + "cityManager/updateCity.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "N"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/city/citylist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function inicityData(pageNumber,pagesize)
{

	var reqPath = basePath+'cityManager/queryCityByPage.action';
	var sendBody = '{"currentPage": 1,"pageSize": 15}';
	var $tab = $('#cityDataTables tbody').html("");

	
	var reqPath = basePath + 'cityManager/queryCityByPage.action';
	var sendBody = '{"currentPage": '+pageNumber+',"pageSize": '+pagesize+'}';
	var $tab = $('#cityDataTables tbody').html("");

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';
				
				$.each(res.data.data, function(i,n)
				{
					var operation = "<button onclick='editcity(\""+n.uuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
									"<button onclick='del(\""+n.uuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					if(n.isClose=='N')
					{
						operation += "<button onclick='closeCity(\""+n.uuid+"\")' type='button' class='btn btn-primary'>关闭</button>";
					}else if (n.isClose=='Y')
					{
						operation += "<button onclick='openCity(\""+n.uuid+"\")' type='button' class='btn btn-primary'>打开</button>";
					}
					rows += tpl_tr.replace(/\$uuid/g, n.uuid).replace(/\$cityName/,
							n.cityName).replace(/\$nickName/, n.nickName).replace(
							/\$cityIntroduce/, n.cityIntroduce).replace(
									/\$disclaimer/, n.disclaimer).replace(
									/\$cityCode/, n.cityCode).replace(
									/\$operation/, operation);
				});
				$tab.append(rows);

				// page
				pagefunction(inicityData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

/**
 * 
 * @METHODNAME: saveLogincity
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveCity()
{
	
	var requsetPath = basePath + "cityManager/insertCity.action";
	var requestData = $('#frm_login_city').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/city/citylist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});

}


function updateCity()
{
	
	var requsetPath = basePath + "cityManager/updateCity.action";
	var requestData = $('#frm_login_city').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/city/citylist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});

}
